windowRes = '''dialog
{ text:'Fit Script', 
  sizePanel: Panel {
    widthCheckbox: Checkbox { 
		text: '↔ W', 
		value: true, 
	}, 
	heightCheckbox: Checkbox { 
		text: '↕ H', 
		value: false, 
	}, 
	swapCheckbox: Checkbox { 
		text: '⇔ S', 
		value: false, 
	}, 
	removeCheckbox: Checkbox { 
		text: '✁ D', 
		value: false, 
	}, 
	orientation: 'row',
	alignment: ['fill','top'],
	text: 'Options',
  },
  alignPanel: Panel {
	hOption: Group {
		leftRadio: RadioButton { 
			text: '← L', 
			value: false, 
		}, 
		rightRadio: RadioButton { 
			text: '→ R', 
		}, 
	  },
	  vOption: Group {
		topRadio: RadioButton { 
			text: '↑ T', 
			value: false, 
		}, 
		bottomRadio: RadioButton { 
			text: '↓ B', 
		}, 
	  },
	  orientation: 'row',
	  alignment: ['fill','top'],
	  text: 'Align',
  },
  
  btnGroup: Group {
    alignment:'center', 
    okBtn: Button { text:'✓  submit', },
    cancelBtn: Button { text:'✕ cancel', }
  }
}'''

var win = new Window(windowRes)


var argument = {widthChecked: true}
win.sizePanel.widthCheckbox.onClick = function() {
	argument.widthChecked = win.sizePanel.widthCheckbox.value
}
win.sizePanel.heightCheckbox.onClick = function() {
	argument.heightChecked = win.sizePanel.heightCheckbox.value
}
win.sizePanel.swapCheckbox.onClick = function() {
	argument.swapChecked = win.sizePanel.swapCheckbox.value
}
win.sizePanel.removeCheckbox.onClick = function() {
	argument.removeChecked = win.sizePanel.removeCheckbox.value
}

win.alignPanel.hOption.leftRadio.onClick = function() {
	argument.leftOn = win.alignPanel.hOption.leftRadio.value
	argument.rightOn = win.alignPanel.hOption.rightRadio.value
}
win.alignPanel.hOption.rightRadio.onClick = function() {
	argument.leftOn = win.alignPanel.hOption.leftRadio.value
	argument.rightOn = win.alignPanel.hOption.rightRadio.value
}
win.alignPanel.vOption.topRadio.onClick = function() {
	argument.topOn = win.alignPanel.vOption.topRadio.value
	argument.bottomOn = win.alignPanel.vOption.bottomRadio.value
}
win.alignPanel.vOption.bottomRadio.onClick = function() {
	argument.topRadio = win.alignPanel.vOption.topRadio.value
	argument.bottomRadio = win.alignPanel.vOption.bottomRadio.value
}

win.btnGroup.cancelBtn.onClick = function() {
	win.close();
}
win.btnGroup.okBtn.onClick = function() {
	var selectedItems = app.activeDocument.selection;
	if ( selectedItems.length > 1 ) {
		var source = argument.swapChecked ? selectedItems[1] : selectedItems[0]
		var target = argument.swapChecked ? selectedItems[0] : selectedItems[1]

		argument.widthChecked && fitWidth(source, target)
		argument.heightChecked && fitHeight(source, target)

		argument.leftOn && alignLeft(source, target)
		argument.topOn && alignTop(source, target)

		argument.removeChecked && target.remove()
	}
	win.close();
}
  
win.show();



function fitWidth(source, target) {
	var sourceRatio = source.width / source.height
	source.width = target.width
	source.height = target.width / sourceRatio
}

function fitHeight(source, target) {
	var sourceRatio = source.width / source.height
	source.height = target.height
	source.width = target.height * sourceRatio
}

function alignTop(source, target) {
	source.top = target.top
}

function alignLeft(source, target) {
	source.left = target.left
}